package com.example.lee.demob;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btnQuery;
    private TextView result;

    private EditText edtStuno;
    private EditText edtName;
    private EditText edtSex;
    private EditText edtAge;
    private Button btnAdd;

    private EditText delStuno;
    private Button btnDel;

    private EditText changeStuno;
    private EditText changeName;
    private EditText changeSex;
    private EditText changeAge;
    private Button changeBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         * 实现跨应用查询数据
         */
        result = findViewById(R.id.result);
        btnQuery = findViewById(R.id.btn_query);
        btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://lee.student/student");
                Cursor cursor = getContentResolver().query(uri,null,null,null,null);

                StringBuffer buffer = new StringBuffer();
                if (cursor != null && cursor.getCount() > 0) {
                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        String stuno = cursor.getString(0);
                        String name = cursor.getString(1);
                        int sex = cursor.getInt(2);
                        int age = cursor.getInt(3);

                        buffer.append(stuno).append("  ").append(name).append("  ").append(sex).append(" ").append(age).append("\n");
                    }
                    result.setText(buffer);
                }else {
                    result.setText("无数据");
                }
            }
        });

        edtStuno = findViewById(R.id.edtStuno);
        edtName = findViewById(R.id.edtName);
        edtSex = findViewById(R.id.edtSex);
        edtAge = findViewById(R.id.edtAge);
        btnAdd = findViewById(R.id.btnAdd);

        /**
         * 实现跨应用添加数据
         */
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String no = edtStuno.getText().toString();
                String name = edtName.getText().toString();
                int sex = Integer.parseInt(edtSex.getText().toString());
                int age = Integer.parseInt(edtAge.getText().toString());

                Uri uri = Uri.parse("content://lee.student/student");
                ContentValues contentValues = new ContentValues();
                contentValues.put("stuno",no);
                contentValues.put("name",name);
                contentValues.put("sex",sex);
                contentValues.put("age",age);
                getContentResolver().insert(uri,contentValues);
            }
        });


        /**
         * 实现跨应用删除数据
         */
        delStuno = findViewById(R.id.delStuno);
        btnDel = findViewById(R.id.btnDel);
        btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String no = delStuno.getText().toString();

                Uri uri = Uri.parse("content://lee.student/student");
                String where = "stuno = " + no;
                int i = getContentResolver().delete(uri,where,null);
                Toast.makeText(MainActivity.this,"删除了" + i + "行数据",Toast.LENGTH_SHORT).show();
            }
        });

        changeStuno = findViewById(R.id.changeStuno);
        changeName = findViewById(R.id.changeName);
        changeSex = findViewById(R.id.changeSex);
        changeAge = findViewById(R.id.changeAge);
        changeBtn = findViewById(R.id.changeBtn);

        changeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String no = changeStuno.getText().toString();
                String name = changeName.getText().toString();
                int sex = Integer.parseInt(changeSex.getText().toString());
                int age = Integer.parseInt(changeAge.getText().toString());


                Uri uri = Uri.parse("content://lee.student/student");
                String where = "stuno = " + no;

                ContentValues value = new ContentValues();
                value.put("name",name);
                value.put("sex",sex);
                value.put("age",age);

                int i = getContentResolver().update(uri,value,where,null);
                Toast.makeText(MainActivity.this,"更新了" + i + "行数据",Toast.LENGTH_SHORT).show();
            }
        });

    }
}
